define(['template','urlSet','jquery'],(template,urlSet) => {
	class Header {
		constructor() {
			this.loading().then(() => {
				this.rendernav().then(() => {
					this.showNavtion()
					this.calcCart()
					this.goTo()
				})
				})  
		}
		
		loading () {
				return new Promise(resolve => {
					$('header').load('/html/header.html',resolve)
				})
			}
			
		showNavtion () {
			$('.left').on('mouseenter','li',function () {
				const id = $(this).attr('data-id')
				$(this).addClass('bc').siblings().removeClass('bc')
				$('.navtion .showNavtion').addClass('ac').siblings().removeClass('ac')
				$.get(urlSet.listUrl+id,resp => {
					const categories = resp.data.categories.slice(0,6),
						  sortTypes = resp.data.sortTypes.slice(0,6)
						this.data = {categories,sortTypes}
						$('.showNavtion').html(template('showNavtionTemplate',{...this.data}))
					
				})
			})
			$('.navtion').on('mouseleave','.showNavtion',function () {
				$('.left li').removeClass('bc')
				$('.navtion .showNavtion').removeClass('ac')
			})
		}
		
		calcCart () {
			let cart = localStorage.getItem('cart')
			if (cart) {
				cart = JSON.parse(cart)
				const total = cart.reduce((count,shop) => count+shop.count,0)
				$('.cart_num').html(total)
			}
		}
		
		goTo() {
			$('.reg').on('click',() => {
				window.open(`/html/register.html`,'_self')
			})
			$('.login').on('click',() => {
				window.open(`/html/login.html`,'_self')
			})
		}
		
		rendernav () {
			return new Promise((reslove,reject) => {
				$.get(urlSet.navUrl,resp => {
					if (resp.code === 200) {
						let typeList = resp.data.list
						typeList = typeList.slice(3,9)
						$('.header .left').html(template('typeList',{typeList}))
						reslove()
					}
					else{reject()}
				})
			})
			
		}
		
	}
	return new Header()
})